package com.itms.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itms.mapper.*;
import com.itms.pojo.*;
import com.itms.pojo.Package;
import com.itms.service.BasicDataService;
import org.springframework.beans.factory.annotation.Autowired;


import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service(delay = -1,timeout = 10000,retries = 1)
public class BasicDataServiceImpl implements BasicDataService {

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private PackageMapper packageMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private CarrierMapper carrierMapper;

    @Autowired
    private PartMapper partMapper;

    @Autowired
    private LocMapper locMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private TableDataSyncMapper tableDataSyncMapper;

    @Override
    public List<Project> getProjectList(Date date) {
        return projectMapper.getProjectList(parseDate(date));
    }

    @Override
    public List<Package> getPackageList(Date date) {
        return packageMapper.getPackageList(parseDate(date));
    }

    @Override
    public List<Supplier> getSupplierList(Date date) {
        return supplierMapper.getSupplierList(parseDate(date));
    }

    @Override
    public List<Carrier> getCarrierList(Date date) {

        return carrierMapper.getCarrierList(parseDate(date));
    }

    @Override
    public List<Carrier> getCarrierListByCarrierCode(String CarrierCode) {
        return carrierMapper.getCarrierListByCarrierCode(CarrierCode);
    }

    @Override
    public List<Part> getPartList(Date date) {
        return this.partMapper.getPartList(parseDate(date));
    }

    @Override
    public List<Customer> getCustomerList(Date date) {
        return this.customerMapper.getCustomerList(parseDate(date));
    }

    @Override
    public List<Loc> getLocList(Date date) {
        return this.locMapper.getLocList(parseDate(date));
    }

    @Override
    public Date getTableSyncByTableName(String tableName) {
        return this.tableDataSyncMapper.findByTableName(tableName);
    }


    @Override
    public List<Part> getPartListByLocProject(String picLoc, String dlvLoc, String projectCode,String partCode) {
        return this.partMapper.getPartListByLocProject( picLoc,dlvLoc,projectCode,partCode);
    }

    private static String parseDate(Date date){
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);//date 换成已经已知的Date对象
        cal.add(Calendar.MINUTE, -1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(format.format(cal.getTime()));
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

}
